package com.example.newsclient;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.ashokvarma.bottomnavigation.BadgeItem;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.newsclient.fragment.FindFragment;
import com.example.newsclient.fragment.MineFragment;
import com.example.newsclient.fragment.NewsFragment;
import com.example.newsclient.fragment.WeChatFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {

    private ViewPager viewPager;
    private BottomNavigationBar bottomNavigationBar;
    private List<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();

        //实现ViewPager页面切换
        fragmentList = new ArrayList<>();
        NewsFragment newsFragment = new NewsFragment();
        WeChatFragment weChatFragment = new WeChatFragment();
        FindFragment findFragment = new FindFragment();
        MineFragment mineFragment = new MineFragment();
        //将碎片放入集合
        fragmentList.add(newsFragment);
        fragmentList.add(weChatFragment);
        fragmentList.add(findFragment);
        fragmentList.add(mineFragment);
        //设置适配器
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setCurrentItem(0);

        //为BottomNavigationBar添加Item选项以及相关属性
        bottomNavigationBar.setTabSelectedListener(this);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.setBarBackgroundColor(R.color.colorPrimary);
        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_launcher_background, "新闻资讯").setActiveColorResource(R.color.WHITE))
                .addItem(new BottomNavigationItem(R.drawable.ic_launcher_background, "微信精选").setActiveColorResource(R.color.WHITE))
                .addItem(new BottomNavigationItem(R.drawable.ic_launcher_background, "发现").setActiveColorResource(R.color.WHITE))
                .addItem(new BottomNavigationItem(R.drawable.ic_launcher_background, "我的").setActiveColorResource(R.color.WHITE))
                .setFirstSelectedPosition(0)
                .initialise(); //所有的设置需在调用该方法前完成

        //联系ViewPager和底部导航栏
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //监听ViewPager的滑动，当滑动到某个页面时，将底部导航栏设置为对应的选项
                bottomNavigationBar.selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 初始化控件
     */
    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottomNaView);
    }

    /**
     * 选项卡切换事件监听
     * @param position
     */
    @Override
    public void onTabSelected(int position) {//未选中 -> 选中
        //监听底部导航栏的点击状态，当点击到某个选项时，将ViewPager设置为对应的页面
        viewPager.setCurrentItem(position);
    }

    @Override
    public void onTabUnselected(int position) {//选中 -> 未选中

    }

    @Override
    public void onTabReselected(int position) {//选中 -> 选中

    }

    /**
     * 自定义ViewPager适配器
     */
    class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }
}
